<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Common\CommonController;
use App\Http\Controllers\Controller;
use DB;
use App\Model\Pic;

class PicController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
         $pic = DB::table('shop_pic')
        ->join('shop_price','shop_pic.gid','=','shop_price.id')
        ->join('shop_goods','shop_price.gid','=','shop_goods.id')
        ->select('shop_pic.id','shop_goods.gname','shop_pic.pic')->paginate(3);
        //$pic = DB::table('shop_pic')->select(['id','gid','pic'])->paginate(3);
        //dd($pic->gid);
        //dd($pic);
        return view('Admin/PicAdmin/pic',['pic'=>$pic]);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($id)
    {
        //
        //dd()
        $pic = DB::table('shop_price')->select(['id','gid','color'])->where('id',$id)->first();
        //dd($pic->id);
        $gid = $pic->gid;
        $goods = DB::table('shop_goods')->select(['gname'])->where('id',$gid)->first();
        //dd($goods->gname);
        return view('Admin/PicAdmin/create',['pic'=>$pic,'goods'=>$goods]);
       
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        //dd($request);
        $color = $request->color;
        $gid = $request->gid;
        //dd($gid);
        //dd($color);
         $common = new CommonController;

        $res = $common->uploadToQiNiu($request, 'pic');
        if ($res) {
            $pic = $res['key'];
        }
       $pic = "http://owy3mobpf.bkt.clouddn.com/".$pic;
       //dd($pic);
       $pic = Pic::insertGetId([
                    'gid'=>$gid,
                    'pic' => $pic
                ]);
         
            if($pic==true){
            return redirect('/admin/Pic/index')->with('msg','添加成功');
             } else {
                     return back()->with('errorTip','添加失败');
            }
               
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $pic = DB::table('shop_pic')->select(['id','pic'])->where('id',$id)->first();
        //dd($edit);
        //dd($pic->pic);
        return view('Admin/PicAdmin/edit',['pic'=>$pic]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $pic = $request->pic;
        //dd($pic);
         if($pic!==null) {

                    $common = new CommonController;

                    $res = $common->uploadToQiNiu($request, 'pic');
                    if ($res) {
                        $pic = $res['key'];
                    }
                   $pic = "http://owy3mobpf.bkt.clouddn.com/".$pic;


                    
                 $bool = DB::table('shop_pic')
                ->where('id',$id)
                ->update([
                        
                        'pic'=>$pic
                        ]);
                return redirect('/admin/Pic/index')->with('msg','图片修改成功');
            }else{
                return back()->with('errorTip','修改失败，您未修改任何文件');
    }
}

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        //dd($id);
        $bool = DB::table('shop_pic')->where('id',$id)->delete();
        //dd($bool);
        if($bool){
                 return redirect('/admin/Pic/index')->with('msg','删除成功');
         }
    }
}
